import Mock from 'mockjs'

const tokens = {
  admin: {
    token: 'admin-token'
  },
  student: {
    token: 'student-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: '管理员',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '管理员'
  },
  'student-token': {
    roles: ['student'],
    introduction: '学生用户',
    avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
    name: '张同学'
  }
}

export default {
  login: config => {
    const { username } = JSON.parse(config.body)
    const token = tokens[username]

    // mock error
    if (!token) {
      return {
        code: 60204,
        message: '账号或密码不正确'
      }
    }

    return {
      code: 200,
      data: token
    }
  },
  getInfo: config => {
    const { token } = JSON.parse(config.body)
    const info = users[token]

    // mock error
    if (!info) {
      return {
        code: 50008,
        message: '登录失败，无法获取用户详细信息'
      }
    }

    return {
      code: 200,
      data: info
    }
  },
  logout: () => {
    return {
      code: 200,
      data: 'success'
    }
  }
}